<?php
/**
 * 这个是输出diy首页的内容版块
 */
global $_GPC, $_W;
$uniacid = $this->_uniacid;
$openid = $_W['openid'];

$params = array( ':uniacid'=> $uniacid, ':issystem'=>0, ':disabled'=>0 );

$all = pdo_fetchall("select * from ".tablename("kbwdl_diy_data")." where uniacid=:uniacid AND issystem=:issystem AND disabled=:disabled order by orderid desc", $params);
 
$block =  array();
/**
 * 获取默认城市
 */
$default_city = pdo_fetchcolumn("select conf_value from ".tablename("kbwdl_config")." where uniacid='$uniacid' and placeid='800'");
if($this->module['config']['isduocity']==1){
  /*查询个人设置的*/
        $ucity = pdo_fetchcolumn("select city_id from ".tablename("kbwdl_share_user")." where uniacid='$uniacid' and openid='$openid'");
        if($ucity){
            $default_city = $ucity;
        }
}

/**
    * 查询是否分销
    */
  $ischarges =   0;
  $ret2 = pdo_fetch("select * from ".  tablename("kbwdl_config")." where uniacid='$uniacid' and placeid=91 and module='kbwdl_shequzufang'");
  if(isset($ret2['conf_value']) && $ret2['conf_value']==1){
     $ischarges =  1;
     $groupid = 0;
     /*查询经纪人状态*/
     $groupid = pdo_fetchcolumn("select groupid from ".tablename('kbwdl_broker')." where  uniacid='$uniacid' and openid='$openid'");
     if(!empty($groupid)){
         $groupid = 1;
     }
  } 
if(is_array($all)){        
foreach($all as $index=> $val){
    /*获取新房内容*/    
    $conf  = unserialize($val['jsondata']);
    $adplace = unserialize($val['placedata']);
        
    $content = array();
    if( !empty($val['placedata']) &&  !empty($val['jsondata']) ){
        /*公共数据*/
        $limit = (isset($adplace['nums']) && !empty($adplace['nums'])) ? intval($adplace['nums']) : 1;
        $conf['nums'] = $limit;
        /**
         * 查询版块数据
         */    
        if($val['sortid']==1){
            $where = " uniacid = :uniacid  and isclose = :isclose "; 
            if($this->module['config']['isduocity']==1 &&  $default_city){
                $default_city = intval($default_city);
                $where .=" and fid= '$default_city' ";
            }
             
            $search = array(':uniacid'=>$uniacid, ':isclose'=>0);
            if($adplace['sale']>0){
                $search[':sale'] = $adplace['sale'];
                $where.= " AND sale = :sale ";
            }
            if($adplace['paymethod']>0){
                $search[':paymethod'] = $adplace['paymethod'];
                $where.= " AND paymethod = :paymethod ";
            }
            $order = array(' order by fcatid desc', ' order by fcatid asc');
            if(isset($adplace['house_order'])){
                $order_str = $order[ $adplace['house_order'] ];
            }
            $fileds = $this->_house_list_field();       
            $sql =  "SELECT $fileds FROM ".tablename("kbwdl_house_info")." where  $where  $order_str limit $limit ";
            $rs  = pdo_fetchall( $sql , $search); 
            $items = array();
            if( !empty($rs)){
            /* 查询附表 management,view*/
                   $newhouse_ids = $dot = '';
                   foreach($rs as $key =>$v){             
                       $newhouse_ids.= $dot.$v['hid'];
                       $dot = ',';
                   } 
                   
                   $house_base = [];
                   if($newhouse_ids){
                       $extends = pdo_fetchall("select id, management,view from ".tablename('kbwdl_house')." where id in ($newhouse_ids) and uniacid='$uniacid'");
                       if(!empty($extends)){
                           foreach($extends as $key =>$v){
                               $house_base[ $v['id']] = $v;
                           }
                       }
                   }
          /*处理信息*/

               foreach($rs as $key =>$value){ 
                   $value['management'] = $house_base[$value['hid']]['management'];
                   $value['view'] = $house_base[$value['hid']]['view'];
                   $value['detail_url'] = "newshop/index?id=".$value['hid'];
                   $items[$key]  = $this->_format_newhouse_item($value, $ischarges, $groupid);
               }   
            }/*单条链接地址*/           
          /*  foreach($rs as $key =>$value){
            
                $value['detail_url'] = "newshop/index?id=".$value['newshouse_id'];
                $items[$key]  = $this->_format_newhouse_item($value);
            }*/
            $conf['sortid'] = $val['sortid'];
            $conf['sortname'] = '新房内容';
            
            /*获取二手房信息*/
        }elseif($val['sortid']==2){
            $where = " uniacid = :uniacid  and isonline = :isonline and isdelete = :isdelete ";
            $search = array(':uniacid'=>$uniacid, ':isonline'=>1, ':isdelete'=>0 );
            
            if($this->module['config']['isduocity']==1 &&  $default_city){
                $default_city = intval($default_city);
                $where .=" and city_id = '$default_city' ";
            }
            /**
             * 规则：rent_type 表示0出售，2出租， zhutype 房源类型： 0 房源，1=写字楼，2=商铺，3=生意，4 需求类。
             * 数组按顺序表示 二手房  租房  商铺出售  商铺出租  写字楼出售  写字楼出租  
             * 生意转让,'厂房出租','厂房出售','仓库出租','仓库出售'的搜索条件
             */
            $house_type_sql = array(
                0=> " AND rent_type = 0 AND zhutype = 0 ",
                1=> " AND rent_type = 2 AND zhutype = 0 ",
                2=> " AND rent_type = 0 AND zhutype = 2 ", 
                3=> " AND rent_type = 2 AND zhutype = 2 ",
                4=> " AND rent_type = 0 AND zhutype = 1 ",
                5=> " AND rent_type = 2 AND zhutype = 1 ",
                6=> " AND rent_type = 0 AND zhutype = 3 ",
                7=> " AND rent_type = 2 AND zhutype = 7 ",
                8=> " AND rent_type = 0 AND zhutype = 7 ",
                9=> " AND rent_type = 2 AND zhutype = 8 ",
                10=> " AND rent_type = 0 AND zhutype = 8 ",
            );
            /*房源类型的条件*/
            if(isset($adplace['house_type'])){
                $type = intval($adplace['house_type']);
                if(!empty($house_type_sql[$type])){
                    $where.= $house_type_sql[$type];
                }                
            }
            /*  房源 推荐*/
            if(isset($adplace['house_pos'])){                
                if(intval($adplace['house_pos']) ==1){
                     $search[':isjingpin'] = 1;
                     $where.= " AND isjingpin = :isjingpin ";
                }elseif(intval($adplace['house_pos']) ==2){
                    $search[':istop'] = 1;
                    $where.= " AND istop = :istop ";
                }else{ }
            }
            /*排序*/
            $tmp = array(
                    0=>" order by add_time desc ", 
                    1=> ' order by update_time desc ',
                    2=>' order by loyer desc'
                );
            $order_str = '';
            if(isset($adplace['house_order'])){
                $order_str = $tmp[ $adplace['house_order'] ];
            } 
                /*按当前用户类型 查看*/
            $permission = " and permission=0 ";
            if($groupid>0){
                     /*经纪人可以查看 permission in (0,1)*/
                  $permission = " and permission < 2 ";
             }
         
            /*查询数据*/
            $fileds =  $this->_query_sechouse_field();
            $sql = "SELECT $fileds  FROM " . tablename("kbwdl_house") . " where $where $permission $order_str limit $limit ";
            $items = pdo_fetchall($sql, $search);        
            if (!empty($items)) {
                foreach ($items as $key => $item) {               
                    $value = $this->_format_sechouse($item);              
                    $items[$key] = $this->_format_show_item($value, $ischarges, $groupid) ;
                 }
            }
            $conf['sortid'] = $val['sortid'];
            $conf['sortname'] = '二手房租房表';
            /**
             * 房产电商的内容
             */
        }elseif($val['sortid']==3){
            $where = " uniacid = :uniacid  and tid = :mid and disabled = :disabled ";
            $search = array(':uniacid'=>$uniacid, ':mid'=>10, ':disabled'=>0 );
            if(isset($adplace['fid']) && $adplace['fid']>0){                
                $where.= " AND fid=:fid "; 
                $search[':fid'] = intval($adplace['fid']);
            }
           /* if(isset($adplace['house_pos'])){ 
                if($adplace['house_pos']==0){
                  $where.= " AND posid = :istop "; 
                  $search[':istop'] = intval($adplace['house_pos']);  
                }
                
            }*/
            $order_str = " order by id desc ";
            if(isset($adplace['house_order'])){                
                if($adplace['house_order']==1){
                    $order_str = " order by orderid desc ";
                } 
            }
            
            $sql = "select id,fid,thumb,title,fname ,addtime ,onclick as click"
                    . " from ".tablename("kbwdl_house_saleinfo")." where $where  $order_str limit $limit ";
            $items = pdo_fetchall($sql, $search);        
            if (!empty($items)) {
                foreach ($items as $key => $item) {               
                    $item['thumb'] = tomedia($item['thumb']) ;
                    $item['addtime2'] = date('Y-m-d', strtotime($item['addtime']) );
                    $item['more'] = $conf['item_link_txt'];
                    $items[$key] = $item ;
                 }
            }
            $conf['sortid'] = $val['sortid'];
            $conf['sortname'] = '新闻资讯';
            
            /**
             * 自定义版块内容
             */
        }elseif($val['sortid']==9){
            $adid = $val['id'];
            $where = " uniacid = :uniacid  and issystem = :issystem and page = :page ";
            $search = array(':uniacid'=>$uniacid, ':issystem'=>1, ':page'=>$adid );
            $sql = "SELECT *  FROM " . tablename("kbwdl_diy_data") . " where $where order by id desc  ";
            $rs = pdo_fetchall($sql, $search);
            $items = array();
            if (!empty($rs)) {
                foreach ($rs as $key => $item) {              
                    $d = unserialize($item['placedata']) ;
                    $d['thumb'] = tomedia($d['thumb']);
                    $d['video'] = tomedia($d['video']);
                    /*根据这个信息类型显示不同模板*/
                    $d['deftype'] = $adplace['house_pos'];
                    if($adplace['house_pos']==0){
                        if(!empty($d['thumb'])){
                            $items[$key] = $d;
                        }
                    }elseif($adplace['house_pos']==1){
                        if(!empty($d['thumb']) &&!empty($d['title']) ){
                            $items[$key] = $d;
                        }
                    }elseif($adplace['house_pos']==2){
                        if( !empty($d['title']) ){
                            $items[$key] = $d;
                        }
                    }elseif($adplace['house_pos']==3){
                        if(!empty($d['thumb']) &&!empty($d['video']) ){
                            $items[$key] = $d;
                        }
                    }
                    
                 }
            }
            $conf['deftype'] = $adplace['house_pos'];
            $conf['sortid'] = $val['sortid'];
            $conf['sortname'] = '自定义内容'; 
        } 
        /*公共数据*/       
         $conf['thumb'] = tomedia($conf['thumb']);
         $blcok[$index]['block'] = $conf;
         $blcok[$index]['content'] = $items;
       
    }  // if !empty(); 
    
}
}
$this->result(0, 'success', $blcok ); 